﻿using GetMan.Core.Common;
using GetMan.Core.DB.Entities;
using GetMan.Core.DB.Services.WorkSpace;
using GetMan.Core.Extensions;
using GetMan.Core.infrastructure.BaseC;
using Rubyer;
using Rubyer.Models;
using System;
using System.ComponentModel.Composition;

namespace GetMan.ViewModels
{
    /// <summary>
    /// 工作空间新增或更新
    /// </summary>
    [Export(typeof(WorkSpaceViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class WorkSpaceViewModel : ViewModelBase<WorkSpaceViewModel>, IDialogDataContext
    {
        public string UniqueID
        {
            get;
        } = Guid.NewGuid().ToString("N");

        [Import]
        private IWorkSpaceService workSpaceSerivce;
        public WorkSpaceViewModel() : base()
        {

        }

        ~WorkSpaceViewModel()
        {

        }

        protected override void OnViewLoaded(object view)
        {
            base.OnViewLoaded(view);

        }

        #region 属性
        /// <summary>
        /// 主键
        /// </summary>
        private long id;
        public long Id
        {
            get => id;
            set
            {
                id = value;
                NotifyOfPropertyChange(() => Id);
            }
        }

        /// <summary>
        /// 名称
        /// </summary>
        private string _workSpaceName;
        public string WorkSpaceName
        {
            get => _workSpaceName;
            set
            {
                _workSpaceName = value;
                NotifyOfPropertyChange(() => WorkSpaceName);
            }
        }

        /// <summary>
        /// 描述
        /// </summary>
        private string _description;
        public string Description
        {
            get => _description;
            set
            {
                _description = value;
                NotifyOfPropertyChange(() => Description);
            }
        }
        #endregion

        private string title;
        public string Title
        {
            get => title;
            set
            {
                title = value;
                NotifyOfPropertyChange(() => Title);
            }
        }

        public event Action<object> RequestClose;

        /// <summary>
        /// 弹窗显示时初始化
        /// </summary>
        /// <param name="parameters"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnDialogOpened(object parameters)
        {
            var @params = (IParameters)parameters;
            var getRet = @params.TryGetValue<long>(DialogCommanKeys.Id, out var currId);
            Id = getRet ? currId : 0;
            var model = workSpaceSerivce.Get(Id);
            if (model != null)
            {
                Title = "工作空间编辑";
                WorkSpaceName = model.WorkSpaceName;
                Description = model.Description;
            }
            else
            {
                Title = "工作空间新增";
            }
        }

        #region 事件
        public void Save()
        {
            if (WorkSpaceName.IsNull())
            {
                Message.Error("工作空间名称不能为空");
                return;
            }

            //更新
            if (Id > 0)
            {
                var entity = workSpaceSerivce.Get(Id);
                entity.WorkSpaceName = WorkSpaceName;
                entity.Description = Description;
                entity.UpdateTime = DateTime.Now;
                int ret = workSpaceSerivce.Update(entity);
                if (ret > 0)
                {
                    Message.Success("更新工作空间成功");
                    Close(true, entity);
                    return;
                }
                Message.Error("更新工作空间失败");
            }
            else
            {
                var entity = new WorkSpaceEntity();
                entity.WorkSpaceName = WorkSpaceName;
                entity.Description = Description;
                entity.UpdateTime = DateTime.Now;
                entity.CreateTime = DateTime.Now;
                entity.IsEnable = true;
                long retId = workSpaceSerivce.Add(entity);
                if (retId > 0)
                {
                    Message.Success("新增工作空间成功");
                    entity.Id = retId;
                    Close(true, entity);
                    return;
                }
                Message.Error("新增工作空间失败");
            }
        }

        public void Close(bool isRefresh, WorkSpaceEntity model = null)
        {
            var parameters = new Parameters();
            //是否刷新
            parameters.Add(DialogCommanKeys.Refresh, isRefresh);
            parameters.Add(DialogCommanKeys.Model, model);
            this.RequestClose?.Invoke(parameters);
        }
        public void Close()
        {
            this.Close(false, null);
        }
        #endregion
    }
}